using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.Globalization;

namespace IGGCore
{
    /// <summary>
    /// The boxscore parser
    /// </summary>
    public class BoxscoreParser : BaseParser
    {
        private CultureInfo encaCulture = new CultureInfo("en-ca");

        /// <summary>
        /// Parse the game boxscore
        /// </summary>
        public void ParseBoxscore(DateTime currentDate, Game game)
        {
            Boxscore boxscore = new Boxscore();

            string id = game.Id.Replace("/", "_");
            id = id.Replace("-", "_");
            string url = CoreConstants.MLB_ONLINE_BASE_URL
                + "/" + ToolBox.GetDateURL(currentDate)
                + "/gid_" + id + "/" + CoreConstants.BOXSCORE_URL;
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load(url);
                XmlNode boxNode = xmldoc.SelectSingleNode("boxscore");
                XmlNodeList battingList = boxNode.SelectNodes("batting");
                XmlNodeList pitchingList = boxNode.SelectNodes("pitching");
                XmlNode infoNode = boxNode.SelectSingleNode("game_info");


                boxscore.GameInfo = infoNode.InnerXml;
                boxscore.Date = GetAttribute(boxNode, "date", "UNK");

                foreach (XmlNode battingNode in battingList)
                {
                    XmlNodeList batters = battingNode.SelectNodes("batter");
                    string kind = GetAttribute(battingNode, "team_flag", "UNK");
                    foreach (XmlNode batterNode in batters)
                    {
                        Player player = new Player();
                        player.LastName = GetAttribute(batterNode, "name", "Unknown");
                        player.Position = GetAttribute(batterNode, "pos", "UNK");
                        player.BO = GetAttribute(batterNode, "bo", "UNK");
                        player.StolenBases = int.Parse(GetAttribute(batterNode, "sb", "0"));
                        player.SeasonHomeruns = int.Parse(GetAttribute(batterNode, "s_hr", "0"));
                        player.SeasonRBI = int.Parse(GetAttribute(batterNode, "s_rbi", "0"));
                        player.NameDisplay = GetAttribute(batterNode, "name_display_first_last", "UNK");
                        player.Ab = int.Parse(GetAttribute(batterNode, "ab", "0"));
                        player.PO = GetAttribute(batterNode, "po", "UNK");
                        player.Id = GetAttribute(batterNode, "id", null);
                        player.R = GetAttribute(batterNode, "r", "UNK");
                        player.Homeruns = int.Parse(GetAttribute(batterNode, "hr", "0"));
                        player.BB = GetAttribute(batterNode, "bb", "UNK");
                        player.Hits = int.Parse(GetAttribute(batterNode, "h", "0"));
                        player.RBI = int.Parse(GetAttribute(batterNode, "rbi", "0"));
                        player.Report = GetAttribute(batterNode, "note", "");
                        player.AVG = GetAttribute(batterNode, "avg", "");
                        if (kind == "away")
                        {
                            boxscore.AwayBatting.Add(player);
                        }
                        else
                        {
                            boxscore.HomeBatting.Add(player);
                        }
                    }
                    XmlNode battingDetailsNode = battingNode.SelectSingleNode("text_data");
                    if (battingDetailsNode != null)
                    {
                        if (kind == "away")
                        {
                            boxscore.AwayBattingDetails = battingDetailsNode.InnerText;
                        }
                        else
                        {
                            boxscore.HomeBattingDetails = battingDetailsNode.InnerText;
                        }
                    }
                }
                foreach (XmlNode pitchingNode in pitchingList)
                {
                    XmlNodeList pitchers = pitchingNode.SelectNodes("pitcher");
                    string kind = GetAttribute(pitchingNode, "team_flag", "UNK");
                    foreach (XmlNode pitcherNode in pitchers)
                    {
                        Player player = new Player();
                        player.LastName = GetAttribute(pitcherNode, "name", "UNK");
                        player.Position = GetAttribute(pitcherNode, "pos", "UNK");
                        player.Outs = GetAttribute(pitcherNode, "out", "0");
                        player.NameDisplay = GetAttribute(pitcherNode, "name_display_first_last", "UNK");
                        player.Hits = int.Parse(GetAttribute(pitcherNode, "h", "0"));
                        player.R = GetAttribute(pitcherNode, "r", "0");
                        player.Id = GetAttribute(pitcherNode, "id", null);
                        player.EarnedRuns = int.Parse(GetAttribute(pitcherNode, "er", "0"));
                        player.Report = GetAttribute(pitcherNode, "note", "");
                        player.BB = GetAttribute(pitcherNode, "bb", "0");
                        player.ERA = GetAttribute(pitcherNode, "era", "0");
                        player.StrikeOuts = int.Parse(GetAttribute(pitcherNode, "so", "0"));

                        float ips = float.Parse(player.Outs);
                        if (ips > 0)
                        {
                            ips = ips / 3;
                        }
                        player.IP = ips.ToString("0.0", encaCulture);
                        if (kind == "away")
                        {
                            boxscore.AwayPitching.Add(player);
                        }
                        else
                        {
                            boxscore.HomePitching.Add(player);
                        }
                    }
                }
            }
            catch (WebException)
            {
                xmldoc = null;
            }
            finally
            {
                xmldoc = null;
            }
            game.Boxscore = boxscore;
        }

    }
}
